<template>
  <a-modal
    :title="title"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">

        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="code"
          required
          label="部门编号">
          <a-input placeholder="请输入部门编号" :disabled="model.id" v-model="model.code"/>
        </a-form-model-item>

        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="name"
          required
          label="部门名称">
          <a-input v-model="model.name"/>
        </a-form-model-item>

        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="parentId"
          label="上级部门">
          <a-tree-select
            v-model="model.parentId"
            style="width: 100%"
            :tree-data="deptTreeData"
            allow-clear
            search-placeholder="请选择上级部门"
          />
        </a-form-model-item>

      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { duplicateCheck } from '@/api/api'
  import { getAction,httpAction } from '@/api/manage'

  export default {
    name: 'DeptInfoModal',
    data() {
      return {
        value: 1,
        title: '操作',
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        validatorRules: {
          name: [{ required: true, message: '请输入部门名称!' }],
          code: [
            { required: true, message: '请输入部门编号!' },
            { validator: this.validateCode }]
        },
        url:{
          add:"/sys/deptInfo/add",
          edit:"/sys/deptInfo/edit",
          deptTreeData:"/sys/deptInfo/queryTreeList"
        },
        deptTreeData:[]
      }
    },
    created() {

    },
    methods: {
      validateCode(rule, value, callback) {
        // 重复校验
        var params = {
          tableName: 'dept_info',
          fieldName: 'code',
          fieldVal: value,
          dataId: this.model.id
        }
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback(res.message)
          }
        })
      },
      add() {
        this.edit({})
      },
      edit(record) {
        this.loadDeptInfoTreeData()
        this.model = Object.assign({}, record)
        this.visible = true
      },
      loadDeptInfoTreeData(){
        getAction(this.url.deptTreeData, {}).then((res) => {
          if (res.success) {
             this.deptTreeData = res.result
          }
        })
      },
      // 确定
      handleOk() {
        const that = this
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }
        })
      },
      // 关闭
      handleCancel() {
        this.close()
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.$refs.form.resetFields();
      }
    }
  }
</script>